package com.ydd.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ydd.moudle.home.StoryDetailActivity;
import com.ydd.util.glide.GlideManager;

import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by tao on 17-3-18.
 */

public class PhotoViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<String> mPhotoList;
    private boolean mIsMatch;

    public PhotoViewPagerAdapter(Context context, List<String> photoList, boolean isMatch) {
        this.mContext = context;
        this.mPhotoList = photoList;
        this.mIsMatch = isMatch;
    }

    @Override
    public int getCount() {
        return mPhotoList.size();
    }

    //构建ViewPager的每一项
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView mPhotoView;
        if (mIsMatch) {
            mPhotoView = new ImageView(container.getContext());
            mPhotoView.setScaleType(ImageView.ScaleType.FIT_XY);
            mPhotoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, StoryDetailActivity.class);
                    intent.putExtra(StoryDetailActivity.STORY_ID, position + "");
                    mContext.startActivity(intent);
                }
            });
        } else {
            mPhotoView = new PhotoView(mContext);
        }
        GlideManager.loadImageView(mContext, mPhotoList.get(position).trim(), mPhotoView);
        container.addView(mPhotoView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        return mPhotoView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
